package com.mm.cloth.utils;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * RecyclerView项目间距装饰器
 */
public class ItemSpacingDecoration extends RecyclerView.ItemDecoration {
    private final int spacing;

    /**
     * 构造函数
     * @param spacing 间距（像素）
     */
    public ItemSpacingDecoration(int spacing) {
        this.spacing = spacing;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        // 添加左右间距，但不为第一项添加左间距，不为最后一项添加右间距
        int position = parent.getChildAdapterPosition(view);
        int itemCount = parent.getAdapter() != null ? parent.getAdapter().getItemCount() : 0;
        
        // 默认所有项目都有右间距
        outRect.right = spacing;
        
        // 如果不是第一项，则添加左间距
        if (position > 0) {
            outRect.left = spacing / 2;
        }
        
        // 如果是最后一项，减少右间距
        if (position == itemCount - 1) {
            outRect.right = spacing / 2;
        }
    }
} 